########################################################################
## Name:        tt-cc++.rb
## Purpose:     compile c source to native program
## Author:      mayingkui
## Created:     Jan 2010
## Copyright:   
## Licence:     BSD licence
########################################################################
require 'pathname'
tmppath = Pathname.new(__FILE__)
tmppath = tmppath + "../../.."

$globalTTRoot = tmppath.to_s
$globalTTLLVMBin = $globalTTRoot + "/build-app/LLVM/bin"
$globalTTStdHome = $globalTTRoot + "/clang-port"
$globalTTClang = $globalTTLLVMBin + "/clang++"
$globalTTLlc = $globalTTLLVMBin + "/llc"
$globalTTStdc = $globalTTStdHome + "/stdc-hook"
$globalTTStdcxx = $globalTTStdHome + "/stdcxx-hook"

require 'tmpdir'
$globalTmpOutPut = Dir.tmpdir() + "/" + rand(1000).to_s

$globalOutPut = nil
$globalSource = nil

bcCmdArray = [] 
bcCmdArray << $globalTTClang
bcCmdArray << "-I" + $globalTTStdc + "/include"
bcCmdArray << "-I" + $globalTTStdcxx + "/include"
bcCmdArray << "-I" + $globalTTStdcxx + "/include/mingw32"

dstGiven = false
ARGV.each {|x| 
	if true == dstGiven then
		dstGiven = false
		$globalOutPut = x
		next
	end
	if "-o" == x then
		dstGiven = true
		next
	end
	if not /-o/ =~ x then
		$globalSource = x
	end
	bcCmdArray << x 
}

if not bcCmdArray.include?("-c") then
	bcCmdArray << "-c"
end

if not bcCmdArray.include?("-emit-llvm") then
	bcCmdArray << "-emit-llvm"
end

bcCmdArray << "-o"
bcCmdArray << $globalTmpOutPut + ".bc"

cmdString = bcCmdArray.join(" ")
p cmdString
ret = system(cmdString)
if false == ret then
	exit(-1)
end

asCmdArray = []
asCmdArray << $globalTTLlc
asCmdArray << "-f"
asCmdArray << $globalTmpOutPut + ".bc"
asCmdArray << "-o"
asCmdArray << $globalTmpOutPut + ".s"

cmdString = asCmdArray.join(" ")
p cmdString
ret = system(cmdString)
if false == ret then
	exit(-1)
end


$globalTTBinutilsBin = $globalTTRoot + "/build-app/binutils/bin"
$globalTTAs = $globalTTBinutilsBin + "/as"
$globalTTLd = $globalTTBinutilsBin + "/ld"

objCmdArray = []
objCmdArray << $globalTTAs
objCmdArray << $globalTmpOutPut + ".s"
objCmdArray << "-o"
objCmdArray << $globalTmpOutPut + ".o"

cmdString = objCmdArray.join(" ")
p cmdString
ret = system(cmdString)
if false == ret then
	exit(-1)
end

if ARGV.include?("-c") then
	require 'fileutils'
	if not $globalOutPut then
		$globalOutPut = $globalSource + ".o"
	else
		$globalOutPut = $globalSource + ".o"
	end
	FileUtils.mv($globalTmpOutPut + ".o", $globalOutPut)
	exit(0)
end

$globalTTLink = $globalTTRoot + "/clang-port/mingw32"

linkCmdArray = []
linkCmdArray << $globalTTLd
linkCmdArray << "-Bdynamic"
linkCmdArray << "-u ___register_frame_info"
linkCmdArray << "-u ___deregister_frame_info"
if $globalOutPut then
	linkCmdArray << "-o"
	linkCmdArray << $globalOutPut
end
linkCmdArray << $globalTTLink + "/crt2.o"
linkCmdArray << $globalTTLink + "/crtbegin.o"
linkCmdArray << $globalTmpOutPut + ".o"
linkCmdArray << "-L" + $globalTTLink
linkCmdArray << "-lstdc++"
linkCmdArray << "-lclang-stdc-hook"
linkCmdArray << "-lmingw32"
linkCmdArray << "-lgcc_eh"
linkCmdArray << "-lgcc"
linkCmdArray << "-lmoldname"
linkCmdArray << "-lmingwex"
linkCmdArray << "-lmsvcrt"
linkCmdArray << "-luser32"
linkCmdArray << "-lkernel32"
linkCmdArray << "-ladvapi32"
linkCmdArray << "-lshell32"
linkCmdArray << "-lmingw32"
linkCmdArray << "-lgcc_eh"
linkCmdArray << "-lgcc"
linkCmdArray << "-lmoldname"
linkCmdArray << "-lmingwex"
linkCmdArray << "-lmsvcrt"
linkCmdArray << $globalTTLink + "/crtend.o"

cmdString = linkCmdArray.join(" ")
p cmdString
ret = system(cmdString)
if false == ret then
	exit(-1)
end
